import { computed, ref, unref } from '@vue/runtime-core';
import { appStore } from '../store/modules/app';
import router from '/@/router';
import { isString } from '/@/utils/is';

function handleError(e: Error) {
  console.error(e);
}

export const getMyIntention = computed(() => {
  return appStore.getMyIntention;
});

export const intentionRef = ref<string[]>(appStore.getMyIntention ? appStore.getMyIntention : []);

export function commitMyIntention(myIntention: string[]) {
  appStore.commitMyIntention(myIntention);
  return getMyIntention;
}

export function useGo() {
  const { push, replace } = router;
  function go(opt: string | object, isReplace = false) {
    if (!opt) return;
    if (isString(opt)) {
      isReplace ? replace(opt).catch(handleError) : push(opt).catch(handleError);
    } else {
      // const o = opt as RouteLocationRaw;
      isReplace ? replace(opt).catch(handleError) : push(opt).catch(handleError);
    }
  }
  return go;
}
